package com.ruicar.afs.cloud.basic.common.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.common.entity.BasicPenaltyInterest;
import com.ruicar.afs.cloud.basic.common.service.BasicPenaltyInterestService;
import com.ruicar.afs.cloud.basic.common.vo.ExpenseVo;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 逾期罚息数据（按照期数维度统计）
 */
@Slf4j
@RestController
@RequestMapping("/penaltyInterest")
@AllArgsConstructor
public class BasicPenaltyInterestController {

    private final BasicPenaltyInterestService basicPenaltyInterestService;

    @ApiOperation(value = "根据合同号码查询罚息列表数据")
    @PostMapping("/getPenaltyInterestList")
    public IResponse<List<BasicPenaltyInterest>> getPenaltyInterestList(@RequestParam("contractNo") String contractNo) {
        List<BasicPenaltyInterest> basicPenaltyInterests = basicPenaltyInterestService.list(Wrappers.<BasicPenaltyInterest>lambdaQuery().
                eq(BasicPenaltyInterest::getContractNo, contractNo));
        return IResponse.success(basicPenaltyInterests);
    }

    @ApiOperation(value = "根据合同号查询差额逾期利息信息")
    @PostMapping("/getPenaltyList")
    public IResponse getPenaltyList(@RequestParam("contractNo") String contractNo) {
        List<BasicPenaltyInterest> list = basicPenaltyInterestService.list(Wrappers.<BasicPenaltyInterest>lambdaQuery().
                eq(BasicPenaltyInterest::getContractNo, contractNo));
        List<ExpenseVo> expenseVoList = new ArrayList<>();
        for (BasicPenaltyInterest penaltyInterest : list) {
            ExpenseVo ExpenseVo = new ExpenseVo();
            ExpenseVo.setTermNo(penaltyInterest.getTermNo());
            ExpenseVo.setPenalty(penaltyInterest.getTotalPenalty().subtract(penaltyInterest.getReceivedAmt().add(penaltyInterest.getExemptedAmt())));
            ExpenseVo.setSubtotal(ExpenseVo.getPenalty());
            expenseVoList.add(ExpenseVo);
        }
        return new IResponse().setData(expenseVoList);
    }
}
